﻿using DelonixRegia.Classes;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DelonixRegia
{
    public partial class ScheduleShift : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Account a = (Account)Session["acc"];

            if (a != null)
            {
                if (a.userType < 2)
                    Response.Redirect("Default.aspx");
            }
            else
            {
                Response.Redirect("SignIn.aspx");
            }
        }

        protected void ddlStaffName_SelectedIndexChanged(object sender, EventArgs e)
        {
            string duty = StaffDBManager.GetDutyTypeByFirstName(ddlStaffName.SelectedItem.Text).ToString();

            if (duty == "1")
            {
                duty = "General Maintenance";
            }
            else if (duty == "2")
            {
                duty = "Room Maintenance";
            }
            else if (duty == "3")
            {
                duty = "Estate Maintenance";
            }
            else
            {
                duty = "Security Maintenance";
            }

            tbxDutyType.Text = duty;
        }

        protected void btnCreateShift_Click(object sender, EventArgs e)
        {
            if ((!string.IsNullOrEmpty(tbxStartDate.Text)) && (!string.IsNullOrEmpty(tbxEndDate.Text)))
            {
                if (ValidateDate(tbxStartDate.Text) && (ValidateDate(tbxEndDate.Text)))
                {
                    string tempStartDateTime = tbxStartDate.Text + " " + ddlStartTime.SelectedValue;
                    string tempEndDateTime = tbxEndDate.Text + " " + ddlEndTime.SelectedValue;

                    string duty = tbxDutyType.Text;

                    if (duty == "General Maintenance")
                    {
                        duty = "1";
                    }
                    else if (duty == "Room Maintenance")
                    {
                        duty = "2";
                    }
                    else if (duty == "Estate Maintenance")
                    {
                        duty = "3";
                    }
                    else
                    {
                        duty = "4";
                    }

                    if (StaffDBManager.createShift(DateTime.Parse(tempStartDateTime), DateTime.Parse(tempEndDateTime), ddlStaffName.SelectedIndex + 1, int.Parse(duty)))
                    {
                        ClientScriptMgr.showSuccessBox("Shift has been successfully created!");
                        tbxStartDate.Text = "";
                        tbxEndDate.Text = "";
                        ddlStartTime.SelectedIndex = 0;
                        ddlEndTime.SelectedIndex = 0;
                        ddlStaffName.SelectedIndex = 0;
                    }
                    else
                    {
                        ClientScriptMgr.showErrorBox("Unable to schedule shift, please try again later!");
                    }
                }
                else
                {
                    ClientScriptMgr.showErrorBox("Invalid date! Please use this format: 31-05-1967");
                }
            }
            else
            {
                ClientScriptMgr.showErrorBox("One or more required fields are empty!");
            }
        }

        private bool ValidateDate(string stringDateValue)
        {
            try
            {
                CultureInfo CultureInfoDateCulture = new CultureInfo("fr-FR");
                DateTime d = DateTime.ParseExact(stringDateValue, "dd-MM-yyyy", CultureInfoDateCulture);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}